<template>
    <el-form ref="loginFormRef" :model="loginForm" @keyup.enter="onLogin">
        <el-form-item prop="username">
            <el-input v-model="loginForm.userAccount" :prefix-icon="User" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="loginForm.userPassword" :prefix-icon="Lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="login-button">
            <el-button type="primary" @click="onLogin()">登录</el-button>
        </el-form-item>
    </el-form>
    <div style="float: right;">
        <el-link>忘记密码？</el-link>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import store from '@/stores';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';

const router = useRouter()
const route = useRoute()
const loginForm = reactive({
    userAccount: '',
    userPassword: ''
})

const onLogin = () => {
    // 用户登录
    store.useUserStore
        .accountLoginAction(loginForm)
        .then(() => {
            refresh()
            ElMessage({
                message: '登录成功',
                type: 'success'
            })
            // location.reload()
            // router.push({ path: '/' })
        })
}

function refresh() {
    // 刷新当前路由
    const { fullPath } = route
    router.replace({
        path: '/redirect' + fullPath
    })
}

</script>

<style lang="scss" scoped>

.login-button {
    :deep(.el-button--primary) {
        margin-top: 10px;
        width: 100%;
        height: 45px;
        font-size: 18px;
        letter-spacing: 8px;
    }
}
</style>